//
//  LTableView.m
//  RuntimeDemo
//
//  Created by 李明明 on 15/8/11.
//  Copyright (c) 2015年 LMM. All rights reserved.
//

#import "LTableView.h"


@interface LTableView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UILabel *label;
@end
@implementation LTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
    }
    return self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.label = [[UILabel alloc] init];
    self.label.text = @"向下拉加载";
    self.label.textAlignment = NSTextAlignmentCenter;
    return self.label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

//
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if ([self.label.text isEqualToString:@"松手开始加载"]) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                // 在这写网络请求
                self.bounds = CGRectMake(0, 40, self.frame.size.width, self.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }else if ([self.label.text isEqualToString:@"向下拉加载"] && self.contentOffset.y < 40){
    [UIView animateWithDuration:0 animations:^{
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.bounds = CGRectMake(0, 40, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            
        }];
    }];
}else {
    
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([self.label.text isEqualToString:@"松手开始加载"]) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
                // 在这写网络请求
                self.bounds = CGRectMake(0, 40, self.frame.size.width, self.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
    
}
// 正在滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.contentOffset.y <= 0) {
        self.label.text = @"松手开始加载";
    }else {
        self.label.text = @"向下拉加载";
    }
}


@end
